<!DOCTYPE html>
<html lang="zh-cn"><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/main.css" rel="stylesheet">

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />

    <title>Go程序的时间 | wiseAI的小站</title>
</head><body><div class="container-fluid">
    <nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <img src="/img/favicon.ico" alt="" width="30" height="24" class="d-inline-block align-text-top">
                wiseAI的小站
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/articles/">文章</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/categories/">分类</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/tags/">关键字</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/post/golang/">golang编程</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/about/">关于</a>
                    </li>
                    
                </ul>
                <form class="d-flex">
                    <input id="search-query" class="form-control me-2" type="search" placeholder="Search for anything..." aria-label="Search">
                </form>
            </div>
        </div>
    </nav>
</div>




<div id="content">
<div class="container article">
	<h1>Go程序的时间</h1>
	<div class="post-meta">
		<div>
			
			
			By wiseai | 2022-08-25 | 2 minutes
		</div>
		<div class="tags">
			
			<a class="btn btn-light links btn-sm" href="/tags/%E7%BC%96%E7%A8%8B/">编程</a>
			
			<a class="btn btn-light links btn-sm" href="/tags/golang/">golang</a>
			
		</div>
	</div>
	<div>
		<div class="article-post">
			<p><strong>Time包定义的类型</strong></p>
<p>Time: 时间类型, 包含了秒和纳秒以及 Location</p>
<p>Month: type Month int 月份.</p>
<p>定义了十二个月的常量</p>
<pre tabindex="0"><code>const (
	January Month = 1 + iota
	February
	March
	April
	May
	June
	July
	August
	September
	October
	November
	December
)
</code></pre><p>Weekday 类型: type Weekday int 周</p>
<p>定义了一周的七天</p>
<pre tabindex="0"><code>const (
	Sunday Weekday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)
</code></pre><p>Duration： type Duration int64 持续时间.</p>
<p>定义了以下持续时间类型.</p>
<p>多用于时间的加减 需要传入Duration做为参数的时候.</p>
<p>可以直接传入 time.Second</p>
<pre tabindex="0"><code>const (
 Nanosecond Duration = 1
 Microsecond   = 1000 * Nanosecond
 Millisecond   = 1000 * Microsecond
 Second    = 1000 * Millisecond
 Minute    = 60 * Second
 Hour     = 60 * Minute
)
</code></pre><p>Location</p>
<p>在time包里有两个时区变量:</p>
<p>time.UTC utc时间</p>
<p>time.Local 本地时间</p>
<p>时间格式化</p>
<p>时间格式Time:</p>
<pre tabindex="0"><code>fmt.Println(time.Now())
// 输出: 2019-04-30 14:41:59.661602 +0800 CST m=+0.000225294

fmt.Println(time.Now().String())
// 输出: 2019-04-30 14:41:59.661826 +0800 CST m=+0.000448434
获取当前时间戳：

// 获取当前unix时间戳（秒）
fmt.Println(time.Now().Unix()) // 输出: 1556615702

// 获取当前unix时间戳（毫秒）
fmt.Println(time.Now().UnixNano() / 1e6) // 输出: 1556615702009

// 获取当前unix时间戳（纳秒）
fmt.Println(time.Now().UnixNano()) // 输出: 1556615702009257000
字符串转化成时间戳:

x := &#34;2018-12-27 18:44:55&#34;
p, _ := time.Parse(&#34;2006-01-02 15:04:05&#34;, x)
fmt.Println( p.Unix() ) // 输出: 1545936295
将当前时间转成年月日时分秒格式:

t = time.Now()
fmt.Println(t.Format(&#34;2006-01-02&#34;))   // 输出: 2019-04-30
fmt.Println(t.Format(&#34;2006-01-02 15:04:05&#34;)) // 输出: 2019-04-30 14:43:26
fmt.Println(t.Format(&#34;2006-01-02 00:00:00&#34;)) // 输出: 2019-04-30 00:00:00
fmt.Println(t.Format(&#34;2006/01/02 15:04&#34;)) // 输出: 2019-04-30 14:43
fmt.Println(t.Format(&#34;2006/Jan/02 15:04&#34;)) // 输出: 2019/Apr/30 17:28

// 指定时间
t2 := time.Date(2019, time.November, 28, 11, 35, 46, 0, time.UTC)
// 返回 Time 类型

fmt.Printf(&#34;=&gt;日期格式: %s\n&#34;, t2.Format(&#34;06/01/02 15:04:05&#34;))
// 输出: =&gt;日期格式: 19/11/28 11:35:46
</code></pre><p>注意：
在Go语言中，&ldquo;Y-m-d H:i:s&quot;&ldquo;yyyy-MM-dd HH:mm:ss&rdquo; 为特定的数字 “2006-01-02 15:04:05”是Go语言的创建时间，且必须为这几个准确的数字。</p>
<p>使用 time.Now().Date() 获取年月日：</p>
<pre tabindex="0"><code>// Date()返回三个参数: 年月日
year1, month1, day1 := time.Now().Date()
 
fmt.Printf(&#34;year: %v, type: %T \n&#34;, year1, year1)
// 输出: year: 2019, type: int
 
fmt.Printf(&#34;month: %v, type: %T \n&#34;, month1, month1)
// 输出: month: April, type: time.Month
 
fmt.Printf(&#34;day: %v, type: %T \n&#34;, day1, day1)
// 输出: day: 30, type: int
</code></pre><p>golang的time.Format设计的和其他语言都不一样, 其他语言总是使用一些格式化字符进行标示, 而golang使用&quot;2006-01-02 15:04:05.999999999 -0700 MST&rdquo;</p>
<pre tabindex="0"><code>// String returns the time formatted using the format string
// &#34;2006-01-02 15:04:05.999999999 -0700 MST&#34;
func (t Time) String() string {
 return t.Format(&#34;2006-01-02 15:04:05.999999999 -0700 MST&#34;)
}
</code></pre><p>例子:</p>
<pre tabindex="0"><code>func nowTime() string {
 return time.Now().Format(&#34;2006-01-02 15:04:05&#34;)
}
</code></pre>
		</div>
	</div>
</div>


<div class="container">
    
    <div class="row">
        
        <div class="col-5">
            <a class="page-link link-dark text-end dh" href="/post/email/">
                <h5>前一篇</h5><br>
                golang使用smtp发送Email
            </a>            
        </div>
        
        <div class="col-2">
        </div>
        
        <div class="col-5">
            <a class="page-link link-dark text-start dh" href="/post/%E4%BB%80%E4%B9%88%E6%98%AF%E9%AB%98%E7%BA%A7%E7%9A%84%E6%AC%B2%E6%9C%9B/">
                <h5>后一篇</h5><br>
                什么是高级的欲望?
            </a>            
        </div>
        
    </div>
    
</div>

        </div><br><br>
<footer class="container">
    <h2>友情链接</h2>
    <hr>
    <nav class="nav nav-pills flex-column flex-sm-row">
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/gnzg/index.html" target="_blank">孤鸟之歌</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/mm/index.html" target="_blank">生成随机字符</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/md/index.html" target="_blank">MarkDown</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/" target="_blank">Gitee</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://github.com/" target="_blank">Github</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyun.com/" target="_blank">阿里云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cloud.tencent.com/" target="_blank">腾讯云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.oschina.net/" target="_blank">OSCHINA开源中国</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/wiseai/the-way-to-go_ZH_CN/blob/master/eBook/directory.md" target="_blank">the way to go</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://topgoer.com/" target="_blank">golang文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://goframe.org/display/gf" target="_blank">GoFrame</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyundrive.com/" target="_blank">阿里云盘</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cn.vuejs.org/" target="_blank">vue3文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://element-plus.gitee.io/zh-CN/" target="_blank">element-plus文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.runoob.com/vue3/vue3-tutorial.html" target="_blank">vue3菜鸟教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://v5.bootcss.com/" target="_blank">bootstrap v5 中文文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.bootstrap.cn/" target="_blank">bootstrap文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://caddy2.dengxiaolong.com/docs/" target="_blank">caddy中文教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.58pic.com/" target="_blank">千图</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://ifonts.com/" target="_blank">iFonts</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://marketing.qiniu.com/" target="_blank">七牛云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.ixigua.com/" target="_blank">西瓜视频</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/yugang/index.html" target="_blank">鱼缸计算</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://justcc.mengkang.net/#/" target="_blank">C语言JustCC</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/pptist/" target="_blank">PPTist</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="/index.xml" target="_blank">RSS</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.kancloud.cn/idcpj/python/418553" target="_blank">参考资料</a>
        
    </nav>

    <div class="copyright text-center">
      <span class="power-by">
        Powered by <a class="links" href="https://gohugo.io" target="_blank">Hugo</a>
    </span>
    <span>|</span>
    <span>
        Theme - <a class="links" href="https://github.com/wiseai-go/blog-hugo" target="_blank">WiseAI</a>
    </span>
    <br>
    <span class="copyright-year">
        &copy;
        
        2017 -
        2023<span>
            陇ICP备15000157号
            
        </span></span>

</div>
</footer>
<script src="/js/bootstrap.bundle.min.js"></script>


</body>
</html>
